package com.example.meituan.activity.man.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;

import com.example.meituan.R;
import com.example.meituan.activity.bean.OrderBean;
import com.example.meituan.activity.bean.OrderDetailsBean;
import com.example.meituan.activity.bean.UserBean;
import com.example.meituan.dao.OrderDao;
import com.example.meituan.dao.OrderDetailsDao;
import com.example.meituan.dao.UserDao;

import java.util.List;

public class OrderListAdapter extends ArrayAdapter<OrderBean> {

    private List<OrderBean> list;
    private OrderDetailsDao orderDetialsDao;
    private Context context;
    String path;
    String Userpath;
    OrderDao orderDao;
    UserDao userDao;
    public OrderListAdapter(@NonNull Context context, List<OrderBean>list) {
        super(context, R.layout.man_order_list);
        this.context=context;
        this.list=list;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup){
        if (orderDao == null) {
            orderDao = new OrderDao();
        }
        if (userDao == null) {
            userDao = new UserDao();
        }

        if(convertView==null){
            LayoutInflater inflater=LayoutInflater.from(getContext());
            convertView=inflater.inflate(R.layout.man_order_list,viewGroup,false);
        }
        if(orderDetialsDao==null){
            orderDetialsDao=new OrderDetailsDao();
        }
        OrderBean order=list.get(position);
        ImageView shopImg=convertView.findViewById(R.id.order_shop_img);
        ImageView UserImg=convertView.findViewById(R.id.order_user_img);
        TextView Username=convertView.findViewById(R.id.order_user_name);
        TextView Userid=convertView.findViewById(R.id.order_user_id);
        TextView Userphone=convertView.findViewById(R.id.order_user_phone);
        TextView sendId=convertView.findViewById(R.id.order_sendId);
        TextView address=convertView.findViewById(R.id.order_address);
        TextView num=convertView.findViewById(R.id.order_num);
        TextView price=convertView.findViewById(R.id.order_price);
        TextView sta=convertView.findViewById(R.id.order_sta);
        TextView time=convertView.findViewById(R.id.order_time);
        TextView shopName=convertView.findViewById(R.id.order_shop_name);
        TextView typeDes=convertView.findViewById(R.id.order_type_des);
        TextView priceRed=convertView.findViewById(R.id.order_price_red);
        address.setText(order.getsOrderAddress());
        sta.setText(order.getsOrderSta());
        time.setText(order.getsOrderTime());
        Userid.setText(order.getsUserId());
        OrderDetailsBean orderDetialsBean=orderDetialsDao.getOrderDetailById(order.getsOrderDetialsId());
        shopName.setText(orderDetialsBean.getName());
        path = orderDetialsBean.getImg();
        Bitmap bitmap= BitmapFactory.decodeFile(path);
        shopImg.setImageBitmap(bitmap);
        int a = Integer.parseInt(orderDetialsBean.getPrice()) * Integer.parseInt(orderDetialsBean.getNum());
        price.setText("￥"+a);
        priceRed.setText("￥"+a);
        num.setText(orderDetialsBean.getNum());
        sendId.setText("订单编号:"+orderDetialsBean.getSendId());
        UserBean userBean =userDao.getUserById(order.getsUserId());
        Userpath=userBean.getsImg();
        Bitmap Userbitmap= BitmapFactory.decodeFile(Userpath);
        UserImg.setImageBitmap(Userbitmap);
        Username.setText(userBean.getsName());
        Userphone.setText(userBean.getsPhone());
        typeDes.setText(orderDetialsBean.getDetailType());
        Button reg = convertView.findViewById(R.id.order_reg);
        if ("未发货".equals(order.getsOrderSta())) {
            reg.setText("发货");
            reg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
                    builder.setTitle("请输入订单编号");
                    final EditText input = new EditText(v.getContext());
                    input.setText(orderDetialsBean.getSendId());
                    builder.setView(input);
                    builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            String orderNumber = input.getText().toString();
                            if (orderNumber.isEmpty()) {
                                Toast.makeText(v.getContext(), "请填写订单编号", Toast.LENGTH_SHORT).show();
                                dialog.cancel();
                            } else {
                                orderDetialsBean.setSendId(orderNumber);
                                orderDetialsDao.updateOrderDetail(orderDetialsBean);
                            }
                        }
                    });

                    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        }
                    });
                    AlertDialog dialog = builder.create();
                    dialog.show();
                    dialog.setCanceledOnTouchOutside(false);
                    return;
                }
            });
        }
        else if ("取消中".equals(order.getsOrderSta())) {

            reg.setText("同意与否");
            reg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
                    builder.setTitle("订单取消请求");
                    builder.setPositiveButton("同意", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                           orderDetialsDao.deleteOrderDetailById(order.getsOrderDetialsId());
                           orderDao.deleteOrder(order.getsId());
                        }
                    });

                    builder.setNeutralButton("拒绝", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            if(orderDetialsBean.getSendId()==null){
                                order.setsOrderSta("未发货");
                                orderDao.updateOrder(order);
                            }
                            else{
                                order.setsOrderSta("已发货");
                                orderDao.updateOrder(order);
                            }
                        }
                    });

                    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        }
                    });

                    AlertDialog dialog = builder.create();
                    dialog.show();

                    dialog.setCanceledOnTouchOutside(false);

                }
            });
        }
        else if(order.getsOrderSta()=="已发货"){
            reg.setVisibility(View.VISIBLE);
            reg.setText("修改订单编号");
            reg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
                    builder.setTitle("请输入新的订单编号");
                    final EditText input = new EditText(v.getContext());
                    input.setInputType(InputType.TYPE_CLASS_TEXT);
                    input.setText(orderDetialsBean.getSendId());
                    builder.setView(input);
                    builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            String newOrderNumber = input.getText().toString();
                            if (newOrderNumber.isEmpty()) {
                                Toast.makeText(v.getContext(), "请填写新的订单编号", Toast.LENGTH_SHORT).show();
                                dialog.cancel();
                            } else {
                                orderDetialsBean.setSendId(newOrderNumber);
                                orderDetialsDao.updateOrderDetail(orderDetialsBean);
                            }
                        }
                    });

                    // 添加取消按钮
                    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        }
                    });

                    // 创建并显示对话框
                    AlertDialog dialog = builder.create();
                    dialog.show();

                    // 确保对话框不会因为点击外部区域而关闭
                    dialog.setCanceledOnTouchOutside(false);

                }
            });
        }

        return convertView;
    }

    @Override
    public int getCount() {
        return list==null?0:list.size();
    }
}
